package com.zeyi.chat.handler;

import com.alibaba.fastjson.JSON;
import com.zeyi.chat.annotation.Mapping;
import com.zeyi.chat.dto.Message;
import com.zeyi.chat.po.ChatGroup;
import com.zeyi.chat.po.User;
import com.zeyi.chat.utils.AvatarUtil;
import com.zeyi.chat.utils.FXMLUtil;
import com.zeyi.chat.manager.StageManager;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @author zeyi
 */
public class QueryHandler {
    private QueryHandler() {
    }

    private static class Singleton {
        public static final QueryHandler INSTANCE = new QueryHandler();
    }

    public static QueryHandler getInstance() {
        return QueryHandler.Singleton.INSTANCE;
    }

    /**
     * 处理查询好友
     */
    @Mapping("queryFriend")
    public void handleFriend(Message message) {
        if (message.getFlag()) {
            User user = JSON.parseObject(message.getData(), User.class);
            display("0", user.getId(), "加好友", user.getAvatar(), user.getUsername(), user.getEmail());
        } else {
            display();
        }
    }

    /**
     * 处理查询群
     */
    @Mapping("queryGroup")
    public void handleGroup(Message message) {
        if (message.getFlag()) {
            ChatGroup group = JSON.parseObject(message.getData(), ChatGroup.class);
            display("1", group.getId(), "加群", group.getAvatar(), group.getGroupName(), group.getGroupCode());
        } else {
            display();
        }
    }

    private void display(String type, Integer id, String btnText, String avatar, String chatName, String account) {
        Platform.runLater(() -> {
            Stage friendStage = StageManager.getFriendStage();
            VBox vBox = (VBox) friendStage.getScene().getRoot();
            Parent root = FXMLUtil.createRoot("/Fxml/FriendItem.fxml");
            ImageView avatarImage = (ImageView) root.lookup("#avatar");
            Label chatNameLabel = (Label) root.lookup("#chatName");
            Label accountLabel = (Label) root.lookup("#account");
            Button addBtn = (Button) root.lookup("#addBtn");
            Label addType = (Label) root.lookup("#addType");
            Label addId = (Label) root.lookup("#addId");
            avatarImage.setImage(new Image(AvatarUtil.suffix + avatar, 70, 70, false, true));
            chatNameLabel.setText(chatName);
            accountLabel.setText("(" + account + ")");
            addBtn.setText(btnText);
            vBox.getChildren().add(root);
            addType.setText(type.equals("0") ? "0" : "1");
            addId.setText(String.valueOf(id));
            friendStage.setHeight(400);
        });
    }

    private void display() {
        Platform.runLater(() -> {
            Label label = new Label("没有找到符合条件的结果");
            FlowPane flowPane = new FlowPane(label);
            flowPane.setAlignment(Pos.CENTER);
            flowPane.setPrefHeight(150);
            Stage friendStage = StageManager.getFriendStage();
            VBox vBox = (VBox) friendStage.getScene().getRoot();
            vBox.getChildren().add(flowPane);
            friendStage.setHeight(400);
        });
    }
}
